var page = 0;
var pageShowCount = 10;
var searchName = "name";
var groupName = "studytypezh";
var groupText = "";
var classifyId = "";
var searchText = "";
var province = "";
var country = "";
var auditsState = "";
var typeid = "";
var lianghui = "";
var shejiren = "";
var state = "";
var date = "";
function onfocusClear(id){

}

/*******************************************************************************
 * 初始化
 */
$(document).ready(function(){
	
	getPageCount();
	
	if(provincename==""){
		getProvinceList();
	}
});
 

function getProvinceList(){
	$.ajax({
   		async:false,
		url:'/admin/user!getAuditsProvinceList.action',
		type:'POST',
		dataType:'json',
		cache: false,
		success:function(data){
			Interceptor(data);
			var types_='provice';
			var resultContent='';
			$(data).each(function(i) {
//				resultContent+='<option value="'+data[i].code+'">'+data[i].shortName+'</option>';
//				resultContent+='<li><a href="#">'+data[i].name+'</a></li>';
				
				var nameid=data[i].code;
				if(i<10){
					resultContent+='<li id="'+nameid+'_fltag"><a href="javascript:void(0);" onclick="selectFenbu(\''+data[i].code+'\')">'+data[i].name+'('+data[i].num+')</a></li>';
				}
				else{
					resultContent+='<li id="'+nameid+'_fltag" class="'+types_+'_fl" style="display:none;"><a href="javascript:void(0);" onclick="selectFenbu(\''+data[i].code+'\')">'+data[i].name+'('+data[i].num+')</a></li>';
				}
				
			});
			if(data.length>10){
				resultContent+='<li id="'+types_+'_more"><a href="javascript:void(0);" onclick="more(\''+types_+'\');">更多...</a></li>';
			}
			$("#proviceList").html(resultContent);
		}
	});
}

function more(types){
	$("."+types+"_fl").show();
	$("#"+types+"_more").html('<a href="javascript:void(0);" onclick="shouqi(\''+types+'\');">收起...</a>');
}

function shouqi(types){
	$("."+types+"_fl").hide();
	$("#"+types+"_more").html('<a href="javascript:void(0);" onclick="more(\''+types+'\');">更多...</a>');
}
function addUser() {
	//window.location.href = basePath + "user_preAddOrUpdate.action";
	window.location.href = basePath + "admin/user/add_admin.jsp";
}

function deleteUser(ids) {
	if ($("[id$='User_CB']:checked").length == 0) {
		alert("请选择需要删除的用户!");
		return false;
	} else {
		if (confirm("确认删除所选项吗?")) {
			$.ajax({
				url : basePath + "/admin/user!deleteUser.action",
				type : "post",
				data : {
					"id" :ids
				},
				dataType : "text",
				beforeSend : parent.msgLoading,
				complete : parent.successLoading,
				success : function(data){
					Interceptor(data);
					if (data=="success") {
						search();
						alert("删除成功!")
					} else {
						alert("删除失败!");
					}
				},
				error : function() {
					alert("删除失败!");
				}
			})
		}
	}
}

function changeCountry(countryid){
	country = countryid;
	if(""==countryid){
		$("#countryAll").addClass("seleA");
	}else{
		$("#countryAll").removeClass("seleA");
	}
	$("[name=countryA]").each(function(){
			$(this).removeClass("seleA");
			if(this.id=="country_"+countryid){
				$(this).addClass("seleA");
			}
	});
	search();
}

function changeState(stateStr){
	state = stateStr;
	search();
}

function changeDate(dateStr){
	date = dateStr;
	search();
}

//点击事件
function searchInstitution(item_id) {
	$("#orgid").val(item_id);
	getPageCount();
}


function getAllOrgByProvince(i,province) {
	$.ajaxq("searchGroupByCount", {
		async : false,
		url : basePath + 'clinical-tral-registry!getAllOrgByProvince.action',
		type : 'post',
		dataType : 'json',
		data : {
			"province" :province
		},
		success : function(data) {
			Interceptor(data);
			if(null!=data){
				var rContent = '<div class="BoxContent"><ul class="list_3">';
				$(data).each(function(i) {
					rContent += ' <li><a href="javascript:void(0);" onclick ="searchInstitution(\'' + data[i].ID + '\');" >'+ data[i].name + '</a></li>';
				});
				$("#box_" + i).after(rContent+'</ul></div>');
			}
		},
		error : function() {
		}
	});
}

function getAllProvince() {
	$.ajaxq("searchGroupByCount", {
		async : false,
		url : basePath + 'clinical-tral-registry!getAllProvince.action',
		type : 'post',
		dataType : 'json',
		data : {
			"state" :state,
			"date" :date
		},
		success : function(data) {
			Interceptor(data);
			if(null!=data){
				var rContent = "";
				$(data).each(function(i) {
					rContent = '<div id="box_' + i + '" class="BoxTitle"><h2>' + data[i].name + ' </h2></div>';
					$("#pro_list_div").append(rContent);
					getAllOrgByProvince(i,data[i].name)
				});
			}
		},
		error : function() {
		}
	});
}

function getAllInstitution() {
	// id=groupContent
	$.ajaxq("searchGroupByCount", {
		async : false,
		url : basePath + 'clinical-tral-registry!getAllInstitution.action',
		type : 'post',
		dataType : 'json',
		data : {
			"state" :state,
			"date" :date
		},
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			if(null!=data){
				var groupContent = "";
				$(data).each(function(i) {
					var dataObj = data[i];
					var item_id = data[i][0];
					var item_name = data[i][1];
					var groupCount = 0;
					groupContent += ' <li><a href="javascript:void(0);" onclick ="searchInstitution(\''+item_id+'\');" >'+item_name + '</a> [更新] [删除] </li>';
				});
				$("#groupContent").html(groupContent);
			}
		},
		error : function() {
		}
	});
}



/*******************************************************************************
 * 加载全部国家
 */
function getAllCountry() {
	var  contentHtml='';
	$.ajaxq("getAllCountry", {
		url : basePath + 'country!getAllCountry.action',
		type : 'post',
		data : {
			"searchText" : searchText,
			"searchName" : searchName
		},
		dataType : 'json',
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			if(null!=data){
				$(data).each(function(i) {
					var dataObj = data[i];
					var id = dataObj.id;
					var name = dataObj.name;
					if(i==27){
					  contentHtml +='<div style="display: none;"  id="expandOrCollapseDiv" >';
					}
					contentHtml +='<li id="country_'+id+'" name="countryA" ><a href="javascript:void(0);"  onclick="changeCountry(\''+id+'\');">'+name+'</a></li>';
				});
				contentHtml += '</div><li><a href="javascript:void(0);" style="color:blue;" id="isZhanShou" onclick=$ExpandOrCollapse("expandOrCollapseDiv","isZhanShou","0")>【收缩】</a></li>';
			  $("#countrys").html(contentHtml);
			}
		},
		error : function() {
			alert("对不起，检索超时！");
		}
	});
	return false;
}


/****
 * 回车判断
 * @param evt
 * @returns
 */
var SubmitOrHidden = function(evt){
    evt = window.event || evt;
    if(evt.keyCode==13){
    	search();
    	asAutoSuggest.clearSuggestions("fade");//AutoSuggest
     }
};
window.document.onkeydown=SubmitOrHidden;
/**************************************************
 * 自动提示
 */
function searchSuggest() 
{
	var baseUrl =basePath + "clinical-tral-registry!autosuggest.action";
	var options = {
		script : function() {
			return baseUrl+"?classifyID=clinicaltralregistry&keyWord="+encodeURI($("#searchText").val());
		},
		varname : "variableName",
		json : true,
		timeout : 3000,
		shownoresults : false,
		maxresults : 35,
		callback: function(record){
			//goselectts(record.id);//联动效果
		}
	};
	var as = new bsn.AutoSuggest('searchText', options);
}
/*******************************************************************************
 * 检索
 */
$(document).ready(function () { 
	  $("#auditsState").bind("change",function(){ 
		auditsState=$(this).val();
	    search();
	});
});
//类型查询
function queryType(typeId){
	typeid = typeId;
	search();
}
//两会
function lianghuiSelect(lianghuiType){
		lianghui =lianghuiType;
		search();
}
//涉及人
function shejirenSelect(shejirenType){
		shejiren =shejirenType;
		search();
}
//查询全部
function queryAll(){
	typeid = '';
	lianghui = '';
	shejiren ='';
	search();
}
function selectFenbu(code){
	province=code;
	search();
}
function search(){
	groupText = '';
	getPageCount();
}
/*******************************************************************************
 * 切换每页显示条数
 */
function changePageShowCount(changeId){
	var selectedVar = $("#"+changeId).find("option:selected");
	pageShowCount = selectedVar.text();
	var selectedIndex = $("#"+changeId).get(0).selectedIndex;
	$("[name=pageShowCount]").get(0).selectedIndex = selectedIndex;//
	$("[name=pageShowCount]").get(1).selectedIndex = selectedIndex;//
	search();
}
/*******************************************************************************
 * 查询记录总数
 */
function getPageCount(){
	searchText = $("#searchText").val();
	auditsState = $("#auditsState").val();
	$.ajaxq("getPageCount", {
		async : false,
		url : '/admin/user!getPageCount.action',
		type : 'post',
		data:{
			"searchText" : searchText,
			"province" : province,
			"auditsState" : auditsState,
			"typeid" : typeid,
			"lianghui" : lianghui,
			"shejiren" : shejiren,
			"state":2
		},
		dataType : 'json',
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			$("[name=countTotal]").html(""+data);
			showPageResult(data);
		},
		error : function() {
			$("[name=countTotal]").html("0");
		}
	});
}
/*******************************************************************************
 * 分页
 */
function showPageResult(count) {
	if (count > 0) {
		$(".pagination").pagination(count, {
			num_edge_entries : 1, // 边缘页数
			num_display_entries : 10, // 主体页数
			prev_text : "上一页",
			next_text : "下一页",
			current_page : page,
			callback : showPageResultCallback,
			items_per_page : pageShowCount
		});
		$(".pagination").show();
	} else {
		$(".pagination").hide();
		var resultContent = '';
		resultContent +='<tr>'
						+'<th scope="col"><input onclick="selectAllUser()" type="checkbox" id="selectAllUser"/></th>'
						+'<th scope="col">用户名/真实姓名</th>'
						+'<th scope="col">所属机构</th>'
						+'<th scope="col">所属省份</th>'
						+'<th scope="col">是否为国家两委局批准的干细胞临床研究机构</th>'
						+'<th scope="col">是否为涉及人的生物医学研究机构</th>'
						+'<th scope="col">用户类型</th>'
						+'<th scope="col">审核状态</th>'
						+'<th scope="col">操作</th>'
					 +'</tr><tr><td colspan="7">暂无信息</td></tr>';
		$("#dataArea").html(resultContent);
	}
}

/*******************************************************************************
 * 返回数据集合
 */
function showPageResultCallback(page_index, jq) {
	var orgid = $("#orgid").val();
	auditsState = $("#auditsState").val();
	$.ajaxq("getPageList", {
		async : false,
		url : '/admin/user!getPageList.action',
		type : 'post',
		data : {
			"pageIndex" : page_index,
			"searchText" : searchText,
			"pageShowCount" : pageShowCount,
			"province" : province,
			"auditsState" : auditsState,
			"typeid" : typeid,
			"lianghui" : lianghui,
			"shejiren" : shejiren,
			"state" :2
		},
		dataType : 'json',
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			showSearchResultContent(data);
		},
		error : function() {
			alert("对不起，检索超时！");
		}
	});
	return false;
}

/*******************************************************************************
 * 全选 反选
 */
function selectAllUser() {
	if ($("#selectAllUser:checked").length==1) {
		$("[id$='User_CB']").attr("checked",'true');
	} else {
		$("[id$='User_CB']").removeAttr("checked");
	}
}

/*******************************************************************************
 * 单选
 */
function selectUser() {
	if ($("[id$='User_CB']:checked").length>0) {
		$("#selectAllUser").attr("checked", 'true');
	} else {
		$("#selectAllUser").removeAttr("checked");
	}
}


/*******************************************************************************
 * 显示数据集合
 */
function showSearchResultContent(data){
	var resultContent = '';
	resultContent +='<tr>'
					+'<th scope="col"><input onclick="selectAllUser()" type="checkbox" id="selectAllUser"/></th>'
					+'<th scope="col">用户名/真实姓名</th>'
					+'<th scope="col">所属机构</th>'
					+'<th scope="col">所属省份</th>'
					+'<th scope="col">是否为国家两委局批准的干细胞临床研究机构</th>'
					+'<th scope="col">是否为涉及人的生物医学研究机构</th>'
					+'<th scope="col">用户类型</th>'
					+'<th scope="col">审核状态</th>'
					+'<th scope="col" width="60">操作</th>'
				 +'</tr>';
	if(null!=data){
		
		var dataState=data.state;
		var dataL=data.datalist;
		
		
		$(dataL).each(function(i) {
			//alert(JSON.stringify(dataL));
			var reasondetailUrl= basePath +"admin/user/user_reason.jsp?id="+dataL[i].id;
			var status = dataL[i].status;
			status = null==status?"":status;
			
			var statusStr = '';
			var operaStr = '';
	        if(status==0) {
	        	statusStr = '<td align="center"><span style="color: red; font-weight: bold;">待审核</span></td>';
	        	operaStr = '<a href="admin/user/user_detail.jsp?id='+dataL[i].id+'">查看详细</a><br>'
	        	+ ' &nbsp;<a href="javascript:void(0);" onclick="passUser(\''+dataL[i].id+'\');">通过</a><br>'
	        	+ ' &nbsp;<a href="'+reasondetailUrl+'">不通过</a>'
	        	;
	        }else if (status==1) {
	        	statusStr = '<td align="center">已通过</td>';
	        	operaStr = '<a href="admin/user/user_detail.jsp?id='+dataL[i].id+'">查看详细</a><br>'
	        	+ ' &nbsp;<a href="admin/user/updata_user.jsp?id='+dataL[i].id+'&username='+encodeURI(dataL[i].username)+'&name='+encodeURI(dataL[i].name)+'">修改密码</a>'
	        	;
	        }else if (status==2) {
	        	statusStr = '<td align="center">不通过</td>';
	        	operaStr = '<a href="admin/user/user_detail.jsp?id='+dataL[i].id+'">查看详细</a>';
	        }
			
	        if(i%2==1){
	        	resultContent +='<tr>';
	        }else{
	        	resultContent +='<tr class="odd">';
	        }
	        //是否为两委
	        var isNationalTwoCommittees;
	        if(dataL[i].isNationalTwoCommittees == '0'){
	        	isNationalTwoCommittees='否';
	        }else{
	        	isNationalTwoCommittees='是';
	        }
	        //是否涉及人
	        var isInvolvedPerson;
	        if(dataL[i].isInvolvedPerson == '0'){
	        	isInvolvedPerson='否';
	        }else{
	        	isInvolvedPerson='是';
	        }
	        var roleType;
	        if(dataL[i].roleType == '0'){
	        	roleType="卫计委管理员";
	        }
	        if(dataL[i].roleType == '1'){
	        	roleType="省级管理员";
	        }
	        if(dataL[i].roleType == '2'){
	        	roleType="机构管理员";
	        }
	        if(dataL[i].roleType == '3'){
	        	roleType="机构用户";
	        }
    	    resultContent +='<td align="center"><input onclick="selectUser()" type="checkbox" id="'+dataL[i].id+'_User_CB" name="usercheckbox" value="'+dataL[i].id+'"></td>'
    	    				+'<td align="center">'+dataL[i].username+'<br/>'+dataL[i].name+'</td>'
							//+'<td align="center">'+dataL[i].name+'</td>'
							+'<td align="center">'+dataL[i].organization+'</td>'
							+'<td align="center">'+dataL[i].province+'</td>'
							+'<td align="center">'+isNationalTwoCommittees+'</td>'
							+'<td align="center">'+isInvolvedPerson+'</td>'
							+'<td align="center">'+roleType+'</td>'
							+statusStr
							+'<td align="center">' + operaStr + '</td>'
						 +'</tr>';
		});
		if(data==''){
			resultContent+='<tr><td colspan="7">暂无信息</td></tr>';
		}
	}else{
		resultContent+='<tr><td colspan="7">暂无信息</td></tr>';
	}
	$("#dataArea").html(resultContent);
}


function passUser(jgid){
	if(confirm("您确定此用户通过审查?")){
		$.ajax({
			async:false,
			url:'/admin/user!saveUserPass.action',
			type:'POST',
			data: {
				"id":jgid
			},
			dataType:'Text',
			cache: false,
			success:function(data){
				Interceptor(data);
				if(data!=null){
					alert("操作成功");
					location.href='/admin/user/user_audit.jsp';
				}
			},
			error:function(){
				//nexts(tag);
			}
		});
	}
}


/*******************************************************************************
 * 分组查询 统计
 */
function searchGroupByCount(){
	searchText = $("#searchText").val();
	$.ajaxq("searchGroupByCount", {
		async : false,
		url : basePath + 'clinical-tral-registry!searchGroupBy.action',
		type : 'post',
		dataType : 'json',
		data : {
			"searchText" : searchText,
			"groupName" : groupName,
			"searchName" : searchName,
			"groupText" : groupText,
			"country" :country,
			"state" :state,
			"date" :date
		},
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			if(null!=data){
				var groupContent = "";
				$(data).each(function(i) {
					var dataObj = data[i];
					var item = dataObj.item;
					var groupCount = dataObj.count;
					groupContent += ' <li><a href="javascript:void(0);" onclick ="searchGroupBy(\''+item+'\');" >'+item+'（'+groupCount+'）</a></li>';
				});
				$("#groupContent").html(groupContent);
			}
		},
		error : function() {
		}
	});
}

/*******************************************************************************
 * 分组查询条目
 */
function searchGroupBy(item){
	groupText = item;
	getPageCount();
}

